﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Specialized;

public partial class Admin_Menu : System.Web.UI.Page
{
    DADataContext da = new DADataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["user"])))
            {
                Response.Redirect("/Online/Dangnhap/");
            }
            else
            {

                var mcn = (from cn in da.Chucnangs where cn.Tenchucnang == "Quản lý menu" select cn.Machucnang).FirstOrDefault();
                var kt = (from a in da.TaikhoanChucnangs where (a.Taikhoan == Session["user"].ToString() && a.Machucnang == mcn) select a).Count();
                if (kt == 1)
                {
                    var load = from mn in da.Menus orderby mn.Vitri select new { mn.Mamenu, mn.Tenmenu, Duongdan = mn.Duongdan.Substring(8), mn.Vitri, mn.Hienthi };
                    gvMenu.DataSource = load;
                    gvMenu.DataBind();
                    if (string.IsNullOrEmpty(Request.QueryString["m"]))
                    {
                        btnSua.Enabled = false;
                        btnThem.Enabled = true;
                    }
                    else
                    {
                        int ma = int.Parse(Request.QueryString["m"]);
                        var get = (from x in da.Menus orderby x.Vitri where x.Mamenu == ma select x).FirstOrDefault();
                        txtten.Text = get.Tenmenu;
                        txtpath.Text = get.Duongdan.Trim().Substring(8);
                        txtvt.Text = get.Vitri.ToString();
                        if (get.Hienthi == 1)
                            ht.Checked = true;
                        else
                            kht.Checked = true;
                        btnSua.Enabled = true;
                        btnThem.Enabled = false;
                    }
                }
                else
                {
                    Response.Redirect("/Online/Admin/Default.aspx");
                }
            }
        }
    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        if (txtten.Text == "" || txtpath.Text == "" || txtvt.Text == "")
        {
            tb.Text = "Nhập đủ 3 thông tin trên!";
        }
        else
        {
            try
            {

                Menu cn = new Menu();
                var layma = (from x in da.Menus where x.Mamenu != 0 orderby x.Mamenu descending select x.Mamenu).FirstOrDefault();
                cn.Mamenu = int.Parse(layma + 1.ToString());
                cn.Tenmenu = txtten.Text.Trim();
                cn.Vitri = double.Parse(txtvt.Text.Trim());
                cn.Duongdan = "/Online/"+txtpath.Text.Trim();
                if (ht.Checked == true)
                    cn.Hienthi = 1;
                else
                    cn.Hienthi = 0;

                da.Menus.InsertOnSubmit(cn);
                da.SubmitChanges();
                var load = from mn in da.Menus orderby mn.Vitri select new { mn.Mamenu, mn.Tenmenu, Duongdan = mn.Duongdan.Substring(8), mn.Vitri, mn.Hienthi };
                gvMenu.DataSource = load;
                gvMenu.DataBind();
                tb.Text = "<blink>Thêm bản ghi thành công!</blink>";
                txtten.Text = "";
                txtpath.Text = "";
                txtvt.Text = "";
            }
            catch (Exception ax)
            {
                tb.Text = ax.Message;
            }
        }
    }
    protected void btnSua_Click(object sender, EventArgs e)
    {
        if (txtten.Text == "" || txtpath.Text == "" || txtvt.Text == "")
        {
            tb.Text = "Nhập đủ 3 thông tin trên!";
        }
        else
        {
            try
            {
                int ma = int.Parse(Request.QueryString["m"]);
                var up = (from p in da.Menus where p.Mamenu == ma select p).Single();
                up.Tenmenu = txtten.Text.Trim();
                up.Duongdan ="/Online/"+ txtpath.Text.Trim();
                up.Vitri = double.Parse(txtvt.Text);
                if (ht.Checked == true)
                    up.Hienthi = 1;
                else
                    up.Hienthi = 0;
                da.SubmitChanges();
                var load = from mn in da.Menus orderby mn.Vitri select new { mn.Mamenu, mn.Tenmenu, Duongdan = mn.Duongdan.Substring(8), mn.Vitri, mn.Hienthi };
                gvMenu.DataSource = load;
                gvMenu.DataBind();
                tb.Text = "<blink>Cập nhật thành công!</blink>";
                txtten.Text = "";
                txtpath.Text = "";
                txtvt.Text = "";
                btnThem.Enabled = true;
                btnSua.Enabled = false;
            }
            catch (Exception ax)
            {
                tb.Text = ax.Message;
            }
        }
    }
    protected void btnXoa_Click(object sender, EventArgs e)
    {
        try
        {
            StringCollection idConnllection = new StringCollection();
            string strids = string.Empty;

            for (int i = 0; i < gvMenu.Rows.Count; i++)
            {
                CheckBox ck = (CheckBox)gvMenu.Rows[i].Cells[0].FindControl("ckq1");
                if (ck != null)
                {
                    if (ck.Checked == true)
                    {
                        Label lb = (Label)gvMenu.Rows[i].Cells[1].FindControl("Ma");
                        strids = lb.Text;
                        idConnllection.Add(strids);
                    }
                }
            }
            if (idConnllection.Count > 0)
            {
                string iDS = ""; int a;
                foreach (string id in idConnllection)
                {
                    iDS += id + ",";
                }
                string[] mang = iDS.Split(',');
                for (int i = 0; i < mang.Length - 1; i++)
                {
                    a = int.Parse(mang[i]);
                    var xoa = da.Menus.SingleOrDefault(p => p.Mamenu == a);
                    if (xoa != null)
                    {
                        da.Menus.DeleteOnSubmit(xoa);
                        da.SubmitChanges();
                    }
                }
                var load = from mn in da.Menus orderby mn.Vitri select new { mn.Mamenu, mn.Tenmenu, Duongdan = mn.Duongdan.Trim().Substring(8), mn.Vitri, mn.Hienthi };
                gvMenu.DataSource = load;
                gvMenu.DataBind();
                tb.Text = "<blink>Xóa thành công!</blink>";
                txtten.Text = "";
                txtpath.Text = "";
                txtvt.Text = "";
            }
            else
                tb.Text = "Không bản ghi nào được chọn để xóa!";
        }
        catch (Exception ax)
        {
            tb.Text = ax.Message;
        }
    }
    protected void btnHuy_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Online/Admin/Menu.aspx");
    }
    
}
